import React, { Component } from 'react'
import request from '../util/request'

export default class Login extends Component {
    state = {
        username:'',
        password:''
    }
    render() {
        let {username,password} = this.state;
        return (
            <div>
                <input type="text" placeholder="请输入用户名" value={username} onChange={this.change} name="username"/>
                <input type="text" placeholder="请输入密码" value={password} onChange={this.change} name="password"/>
                <button onClick={this.login}>登录</button>
            </div>
        )
    }

    //change

    change = (e) => {
        this.setState({
            [e.target.name]:e.target.value
        })
    }

    login = () => {
        let {username,password} = this.state;
        request.post('/api/login',{username,password}).then(res => {
            console.log(res);
            if(res.code === 1){
                alert('登录成功');
                localStorage.setItem('token',res.token);
                this.props.history.push('/home');
            }
        })
    }

}
